//
//  LANBViewController.m
//  LanBKit
//
//  Created by v on 07/15/2020.
//  Copyright (c) 2020 v. All rights reserved.
//

#import "LANBViewController.h"
#import "LANBDepositController.h"
#import "LanBSocketCppController.h"
#import "LANBWebPController.h"
#import <HttpServerDebug/HttpServerDebug.h>

static NSString *const KTitle = @"KTitle";
static NSString *const KController = @"KController";
@interface                                 LANBViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray     *aryData;
@end

@implementation LANBViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = UIColor.blackColor;
    self.aryData = @[ @{KTitle:@"支付demo", KController : @"LANBDepositController"},
                      @{KTitle:@"Socket.io cpp", KController : @"LanBSocketCppController"},
                      @{KTitle:@"WebP", KController : @"LANBWebPController"},
                      @{KTitle:@"Crash Report", KController : @"LANBCrashReportController"},
                      @{KTitle:@"HttpServerDebug", KController : @"HSDHttpServerControlPannelController"}
    ];
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.aryData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    NSInteger        t       = indexPath.row % 2;
    cell.backgroundColor     = t == 0 ? UIColor.grayColor  : UIColor.whiteColor;
    cell.textLabel.textColor = t == 0 ? UIColor.whiteColor : UIColor.blackColor;
    
    cell.textLabel.text   = self.aryData[indexPath.row][KTitle];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    Class c          = NSClassFromString(self.aryData[indexPath.row][KController]);
    UIViewController *vc = [[c alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - set get
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView                 = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.delegate        = self;
        _tableView.dataSource      = self;
        [_tableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"UITableViewCell"];
        [self.view addSubview:_tableView];
        @weakify(self)
        [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
          @strongify(self)
          CGFloat top = UIApplication.sharedApplication.statusBarFrame.size.height + self.navigationController.navigationBar.frame.size.height;
          make.top.equalTo(self.view).offset(top);
          make.left.equalTo(self.view).offset(20.f);
          make.bottom.equalTo(self.view).offset(-50.f);
          make.right.equalTo(self.view).offset(-20.f);
        }];
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
